package com.youdianda.service.impl;



import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youdianda.dto.MenuDTO;
import com.youdianda.vo.MenuQueryVO;
import com.youdianda.entity.SysMenu;
import com.youdianda.mapper.SysMenuMapper;
import com.youdianda.result.Result;
import com.youdianda.service.ISysMenuService;
import com.youdianda.vo.MenuVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author mosen
 * @since 2023-05-15
 */
@Service
@Slf4j
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    /**所有菜单*/
    @Override
    public List<MenuQueryVO> listAllMenu(MenuQueryVO menuQueryVO) {
        if (menuQueryVO==null){
            return null;
        }

        //第一次进入循环,此时vo的id是0，根据vo.getId=pid来查一个vo集合
        List<SysMenu> menuList = sysMenuMapper.selectMenusByPidAndMenuQueryVOId(menuQueryVO);
        if (menuList==null||menuList.size()==0){
            return null;
        }
        List<MenuQueryVO> voList = new ArrayList<>();
        for (SysMenu sysMenu : menuList) {
            MenuQueryVO queryVO = new MenuQueryVO();
            BeanUtils.copyProperties(sysMenu,queryVO);
            queryVO.setIs_show(sysMenu.getIsShow());
            queryVO.setIs_cache(sysMenu.getIsCache());
            queryVO.setIs_link(sysMenu.getIsLink());

            queryVO.setDel_flag(sysMenu.getDelFlag());
            queryVO.setDefault_data(sysMenu.getDefaultData());
            queryVO.setSort(Integer.parseInt(sysMenu.getSort().toString()));

            voList.add(queryVO);
        }

        //遍历该集合,使每个VO设置相对应的children

        for (MenuQueryVO queryVO : voList) {
            queryVO.setChildren(listAllMenu(queryVO));
        }

        return voList;
    }

    /**添加菜单*/
    @Override
    @Transactional
    public Result addMenu(MenuDTO menuDTO) {
        if (menuDTO ==null){
            return Result.error("未知错误");
        }
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(menuDTO,sysMenu);
        sysMenu.setIsShow(menuDTO.getIs_show());
        sysMenu.setIsCache(menuDTO.getIs_cache());
        sysMenu.setIsLink(menuDTO.getIs_link());
        BigDecimal sort = new BigDecimal(Integer.parseInt(menuDTO.getSort().toString()));
        sysMenu.setSort(sort);
        sysMenu.setCreateDate(LocalDateTime.now());
        sysMenu.setUpdateDate(LocalDateTime.now());
        sysMenu.setDelFlag(0);
        sysMenu.setDefaultData("1");
        sysMenuMapper.addMenu(sysMenu);
        return Result.success();
    }

    /**菜单详情*/
    @Override
    public Result<MenuVO> getMenuById(Integer id) {
        if (id==null){
            return Result.error("菜单详情,传入id有误");
        }
        SysMenu sysMenu = sysMenuMapper.getMenuById(id);
        if (sysMenu==null){
            return Result.error("菜单详情出现问题");
        }
        MenuVO menuVO = new MenuVO();
        BeanUtils.copyProperties(sysMenu,menuVO);
        menuVO.setIs_show(sysMenu.getIsShow());
        menuVO.setIs_cache(sysMenu.getIsCache());
        menuVO.setIs_link(sysMenu.getIsLink());
        menuVO.setCreate_date(sysMenu.getCreateDate());
        menuVO.setUpdate_date(sysMenu.getUpdateDate());
        menuVO.setDel_flag(sysMenu.getDelFlag());
        menuVO.setDefault_data(sysMenu.getDefaultData());
        menuVO.setSort(Integer.parseInt(sysMenu.getSort().toString()));
        return Result.success(menuVO);
    }

    /**菜单修改*/
    @Override
    @Transactional
    public Result update(MenuDTO menuDTO) {
        if (menuDTO==null){
            return Result.error("菜单修改出现问题");
        }
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(menuDTO,sysMenu);
        sysMenu.setIsShow(menuDTO.getIs_show());
        sysMenu.setIsCache(menuDTO.getIs_cache());
        sysMenu.setIsLink(menuDTO.getIs_link());
        sysMenu.setDelFlag(menuDTO.getDel_flag());
        sysMenu.setDefaultData(menuDTO.getDefault_data());
        sysMenu.setCreateDate(menuDTO.getCreate_date());
        sysMenu.setUpdateDate(LocalDateTime.now());
        sysMenu.setSort(new BigDecimal(Integer.parseInt(menuDTO.getSort().toString())));
        sysMenuMapper.updateMenu(sysMenu);
        return Result.success();
    }

    /**菜单删除*/
    @Override
    @Transactional
    public Result deleteMenu(Integer id) {
        if (id==null){
            return Result.error("菜单删除,传入id有误");
        }
        List<Integer> list = new ArrayList<>();
        list.add(id);
        deleteMenusByIds(list);
        return Result.success();
    }

    /**菜单删除的嵌套方法*/
    @Transactional
    public void deleteMenusByIds(List<Integer> ids){
        if (ids.size()==0||ids==null){
            return;
        }
        for (Integer id : ids) {
            sysMenuMapper.deleteById(id);
            List<Integer> idsList = sysMenuMapper.getIdsByPid(id);
            deleteMenusByIds(idsList);
        }
    }

    /**对指定ID的menu进行查询其所有子菜单返回单个MenuQueryVO的嵌套方法*/
    public MenuQueryVO listAllMenuById(Integer id){
        SysMenu sysMenu = sysMenuMapper.getMenuById(id);
        MenuQueryVO menuQueryVO = new MenuQueryVO();
        BeanUtils.copyProperties(sysMenu,menuQueryVO);
        menuQueryVO.setIs_show(sysMenu.getIsShow());
        menuQueryVO.setIs_cache(sysMenu.getIsCache());
        menuQueryVO.setIs_link(sysMenu.getIsLink());
        menuQueryVO.setDel_flag(sysMenu.getDelFlag());
        menuQueryVO.setDefault_data(sysMenu.getDefaultData());
        menuQueryVO.setCreate_date(sysMenu.getCreateDate().toString());
//        menuQueryVO.setUpdate_date(sysMenu.getUpdateDate().toString());
        menuQueryVO.setSort(Integer.parseInt(sysMenu.getSort().toString()));

        List<SysMenu> menus = sysMenuMapper.selectMenusByPidAndMenuQueryVOId(menuQueryVO);
//        if (menus.size()==0 || menus==null){
//            return null;
//        }
        List<MenuQueryVO> voList = new ArrayList<>();
        for (SysMenu menu : menus) {
            voList.add(listAllMenuById(menu.getId()));
        }
        menuQueryVO.setChildren(voList);
        return menuQueryVO;
    };
}
